<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class ForaPage {

	private $alerter;

	public function __construct() {
		$this->alerter = new Alerter();	
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		check_access("READ");
	}

	function page_inc_execute_action($page, $action) {
		$THISDIR = $page->directory_tree[$page->dir_depth-1];

		switch($action) {
			case "markread-sub":
				if (!$_SESSION['user_id'])
					errorpage(6);
				if ($page->id != 0) { // alle fora
					$from = array("ITEM_IN INNER JOIN SUB_IN ON ITEM_IN.sub_id = SUB_IN.sub_id", "DUMMY");
					$where = "SUB_IN.dir_id IN (" . implode(",", $page->menu[$page->id]['children']) . ")";
					$sql = db_query_select(array(array(), "ITEM_IN.item_id"), $from, $where);
				} else
					$sql = db_query_select(array(array("item_id")), array("ITEM_IN"), 1);
				$sqlresult = db_do_query($page, $sql);
				$values = array();
				while ($r = db_fetch_array($sqlresult))
					array_push($values, array($r['item_id'], $_SESSION['user_id']));
				if (count($values)) {
					$sql = db_query_insert_multiple(array(item_id, user_id), $values, "HAS_READ", 1);
					db_do_query($page, $sql);
				}
				reset_menu_new($THISDIR['dir_id']);
				$menu = $page->menu[$page->id];
				$i = $menu['ignore'] ? 1 : 0;
				reset($menu['children']);
				do {
					$kid = current($menu['children']);
					$i += $page->menu[$kid]['ignore'];
				} while (next($menu['children']));
				if ($i < 1)
					$this->alerter->add_alert("Als bepaalde directories je niet interesseren, kan je ze negeren in het actiemenu in die directory. " . 
						  "In het menu verschijnt dan niet meer telkens de melding dat er niets nieuws is gebeurd en zo bespaar " . 
						  "je heel wat pagina-berekeningstijd");
				$this->alerter->add_alert("alles gelezen hier...");
				break;
			default:
				$this->alerter->add_alert("unknown action: $action");
				break;
		}
	}

	function reset_menu_new($page, $dir) {
		$subs = $page->menu[$dir]['children'];
		reset($subs);
		do {
			$sub = current($subs);
			$page->menu[$sub]['new_items'] = 0;
			if (is_array($page->menu[$sub]['children']) and count($page->menu[$sub]['children']))
				reset_menu_new($sub);
		} while (next($subs));
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {

		$page->content['title'] = "Subfora";

		if ($_SESSION['user_id'])
			// mark all items read
			add_action('markread-sub', 'url-action', 'markread-sub');

		$forum = new Forum();
		if ($page->id == 0)
			$page->subfora = $forum->fora_fetch_multiple($page->id, $page->menu, array());
		else
			$page->subfora = array(array('fora' => $forum->fora_get_subfora($page->id, $page->menu), 'title' => $page->menu[$page->id]['title']));
	}


	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {

		if ($page->subfora)
			print_subfora($page->subfora, $page->dir_path, 1);
		
	}
}

?>
